//
//  GameScene.m
//  momo
//
//  Created by HappyMilk 02 on 2017/5/1.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "GameScene.h"
#import "PublicTool.h"
#import "LoginHeader.h"

@implementation GameScene {
    
    SKSpriteNode *background;
    
    
}
- (CGFloat )random{
    return (CGFloat)((CGFloat)arc4random() / 0xFFFFFFFF);
}
- (CGFloat )randomeBy:(CGFloat )min and:(CGFloat )max{
    return [self random] * (max - min) + min;
}

- (void)didMoveToView:(SKView *)view {
    // Setup your scene here
    NSLog(@"============");
    [self loadBackground];
    
    [self runAction:[SKAction repeatActionForever:[SKAction sequence:@[[SKAction runBlock:^{
        [self addPlanNodes];
    }],[SKAction waitForDuration:[self randomeBy:1.0 and:7.0]]]]]];
    [self runAction:[SKAction repeatActionForever:[SKAction sequence:@[[SKAction runBlock:^{
        [self addCloudsNode];
    }],[SKAction waitForDuration:20]]]]];
}

- (void)loadBackground{
    
    //SKTexture *texture1 = [SKTexture textureWithImage:[UIImage imageNamed:@"mine"]];
    SKSpriteNode *mineNode = [SKSpriteNode spriteNodeWithImageNamed:@"mine"];
    mineNode.position = CGPointMake(40, __kheight-40);
    mineNode.zPosition = 111;
    mineNode.name = @"mine";
    [self addChild:mineNode];
    
    SKSpriteNode *sendNode = [SKSpriteNode spriteNodeWithImageNamed:@"send"];
    sendNode.position = CGPointMake(__kwidth-40, __kheight-40);
    sendNode.zPosition = 113;
    sendNode.name = @"send";
    [self addChild:sendNode];
    
    SKTexture *texture = [SKTexture textureWithImage:[UIImage imageNamed:@"home_bg"]];
    background = [SKSpriteNode spriteNodeWithTexture:texture size:self.size];
    background.position = CGPointMake(self.size.width/2, self.size.height/2);
    background.zPosition = 0;
    [self addChild:background];
}
- (void)addPlanNodes{
    
    int high = __kheight/3 + (int)(arc4random() % (int)(__kheight-150-__kheight/3));
    NSArray *arr = @[@"plan1",@"plan2"];
    SKSpriteNode *planNode = [SKSpriteNode spriteNodeWithImageNamed:arr[arc4random()%2]];
    planNode.position = CGPointMake(-50, high);
    planNode.zPosition = 100;
    planNode.name = @"plan";
    [self addChild:planNode];
    
    CGFloat time = [self randomeBy:5 and:15];
    SKAction *move = [SKAction moveToX:__kwidth+50 duration:time];
    SKAction *moveDone = [SKAction removeFromParent];
    [planNode runAction:[SKAction sequence:@[move,moveDone]]];
    
}
- (void)addCloudsNode{
    
    int high = __kheight/3 + (int)(arc4random() % (int)(__kheight-150-__kheight/3));
    NSArray *arr = @[@"cloud2",@"cloud3"];
    SKSpriteNode *cloud = [SKSpriteNode spriteNodeWithImageNamed:arr[arc4random()%2]];
    cloud.position = CGPointMake(0, high);
    cloud.zPosition = 80;
    [self addChild:cloud];
    
    SKAction *move = [SKAction moveToX:__kwidth duration:[self randomeBy:30 and:50]];
    SKAction *moveDone = [SKAction removeFromParent];
    [cloud runAction:[SKAction sequence:@[move,moveDone]]];
}

static bool is_end = YES;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touche = [touches.allObjects firstObject];
    CGPoint location = [touche locationInNode:self];
    
    SKNode *node = [self nodeAtPoint:location];
    if ([node.name  isEqual: @"plan"]) {
        //NSLog(@"点击 ----");
        
        
        if (is_end) {
            is_end = NO;
            
            NSString *burstPath = [[NSBundle mainBundle] pathForResource:@"star" ofType:@"sks"];
            SKEmitterNode *burstNode = [NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
            burstNode.position = node.position;
            burstNode.zPosition = 90;
            [self addChild:burstNode];
            
            [self runAction:[SKAction repeatAction:[SKAction sequence:@[[SKAction runBlock:^{
                
                NSString *burstPath = [[NSBundle mainBundle] pathForResource:@"star" ofType:@"sks"];
                SKEmitterNode *burstNode = [NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
                burstNode.position = node.position;
                burstNode.zPosition = 90;
                [self addChild:burstNode];
                
            }],[SKAction waitForDuration:0.1]]] count:10]];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [burstNode runAction:[SKAction removeFromParent]];
                self.tapNodeAction(@"plan");
                is_end = YES;
                
            });
            
            
            
        }
    }else if ([node.name isEqual:@"mine"]){
        self.tapNodeAction(@"mine");
    }else if ([node.name isEqual:@"send"]){
        self.tapNodeAction(@"send");
    }
}


-(void)update:(CFTimeInterval)currentTime {
    // Called before each frame is rendered
}

@end
